<?php
namespace app\common\model;

use think\Model;

/**
 * 地区信息模型
 * Class AdminUser
 * @package app\common\model
 */
class Region extends Model
{

    /**
     * 查询一条用户信息
     *
     * @param array $params
     * @param bool $fields
     * @return array
     */
    public function getInfo(array $params, $fields = true)
    {
        $where = [];

        if (isset($params['region_id'])) {
            $where['region_id'] = $params['region_id'];
        }

        if (isset($params['parent_id']) && $params['parent_id'] > 0) {
            $where['parent_id'] = $params['parent_id'];
        }

        if (isset($params['region_name']) && !empty($params['region_name'])) {
            $where['region_name'] = ['like', '%'.$params['region_name'].'%'];
        }

        if (isset($params['region_type']) && is_numeric($params['region_type'])) {
            $where['region_type'] = $params['region_type'];
        }

        return $this->where($where)->field($fields)->find();
    }

    /**
     * 添加一条数据
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $data
     * @return  integer|boolean
     */
    public function addData(array $data)
    {
        $img_data = $data;

        //不要在同一个实例里面多次新增数据，如果确实需要多次新增，那么可以如下指定
        $res = $this->allowField(true)->data($img_data,true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;

        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['region_id']) && !empty($params['region_id'])) {
            if (is_array($params['region_id'])) {
                $where['region_id'] = ['in', $params['region_id']];
            } else {
                $where['region_id'] = $params['region_id'];
            }
        }

        if (isset($params['region_name']) && !empty($params['region_name'])) {
            $where['region_name'] = ['like', '%' . $params['region_name'] . '%'];
        }

        if (isset($params['parent_id']) && $params['parent_id'] > 0) {
            $where['parent_id'] = $params['parent_id'];
        }

        if (isset($params['region_type']) && is_numeric($params['region_type'])) {
            $where['region_type'] = $params['region_type'];
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'first_char asc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit);
        $data = $data->toArray();

        return $data;
    }

    /**
     * @param $id
     * @param array $req
     * @return bool
     */
    public function saveData($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }
        //$this->data = [];
        $data = $req;
        $res = $this->save($data, ['region_id' => $id]);
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * 取得地区列表
     *
     * @param int $parent_id
     *        大于等于0表示取某个地区的下级地区，小于0表示取所有地区
     * @param string | boolean $fields
     * @param $limit
     * @return array
     */
    public function getListByPid($parent_id = -1, $fields = true, $limit = 2000) {
        if($parent_id >= 0) {
            $regions = $this->field($fields)->where(array('parent_id' => $parent_id))->order('sort_order, first_char')->paginate($limit);
        } else {
            $regions = $this->field($fields)->order('sort_order, first_char')->paginate($limit);
        }
        return $regions;
    }

    /**
     * 取得地区列表数组
     *
     * @param int $parent_id
     *        大于等于0表示取某个地区的下级地区，小于0表示取所有地区
     * @param $fields
     * @return array
     */
    public function getArrayListByPid($parent_id = 0, $fields = true) {
        $regions = $this->field($fields)->where(array('parent_id' => $parent_id))->order('sort_order, first_char')->paginate(2000);
        $regions = $regions->toArray();
        return $regions['data'];
    }
}